package com.drinks.yiniang.io.api;

import com.alibaba.fastjson.JSON;
import com.drinks.yiniang.entity.shop.MfrsDetailJson;
import com.drinks.yiniang.entity.shop.ShopJson;
import com.drinks.yiniang.entity.user.milepost.MilepostCategoryJson;
import com.drinks.yiniang.entity.user.milepost.MilepostCategoryUnlockJson;
import com.drinks.yiniang.entity.user.milepost.MilepostShareJson;
import com.drinks.yiniang.entity.user.milepost.MilepostUnlockJson;
import com.drinks.yiniang.entity.user.milepost.WineAnalyseJson;
import com.drinks.yiniang.io.http.HttpUrls;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.utils.LogUtils;
import com.hansen.library.utils.StringUtils;

/**
 * 里程api
 * Created by han on 2019/3/6 0006.
 */

public class MilepostApiIO extends BaseApiIO {
    volatile static MilepostApiIO instance;

    static public MilepostApiIO getInstance() {
        if (instance == null) {
            synchronized (MilepostApiIO.class) {
                if (instance == null) {
                    instance = new MilepostApiIO();
                }
            }
        }
        return instance;
    }

    /**
     * 里程碑数据
     *
     * @param callback
     */
    public void getMilepostData(final APIRequestCallback<WineAnalyseJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        MainApiIO.getInstance().postRequest(HttpUrls.API_MILEPOST_DATA, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("里程碑数据===onSuccess");

                WineAnalyseJson mDataJson = JSON.parseObject(res, WineAnalyseJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 里程碑最近解锁列表
     *
     * @param page     页码
     * @param callback
     */
    public void getMilepostRecentUnlockList(int page, final APIRequestCallback<MilepostUnlockJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("page", String.valueOf(page));
        paramsMap.put("per_page", Constants.PAGE_SIZE);

        MainApiIO.getInstance().postRequest(HttpUrls.API_MILEPOST_RECENT_UNLOCK_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("里程碑最近解锁列表===onSuccess");

                MilepostUnlockJson mDataJson = JSON.parseObject(res, MilepostUnlockJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null && mDataJson.getData().getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 里程碑分类数据
     *
     * @param categoryId 分类id
     * @param callback
     */
    public void getMilepostCategoryData(String categoryId, final APIRequestCallback<MilepostCategoryJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("category_id", categoryId);

        MainApiIO.getInstance().postRequest(HttpUrls.API_MILEPOST_CATEGORY_DATA, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("里程碑分类数据===onSuccess");

                MilepostCategoryJson mDataJson = JSON.parseObject(res, MilepostCategoryJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 里程碑分类列表
     *
     * @param categoryId 分类id
     * @param page     页码
     * @param callback
     */
    public void getMilepostCategoryList(String categoryId, int page, final APIRequestCallback<MilepostCategoryUnlockJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("category_id", categoryId);
        paramsMap.put("page", String.valueOf(page));
        paramsMap.put("per_page", Constants.PAGE_SIZE);

        MainApiIO.getInstance().postRequest(HttpUrls.SPI_MILEPOST_CATEGORT_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("里程碑分类列表===onSuccess");

                MilepostCategoryUnlockJson mDataJson = JSON.parseObject(res, MilepostCategoryUnlockJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null && mDataJson.getData().getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 里程碑分享数据
     *
     * @param callback
     */
    public void getMilepostShareData(final APIRequestCallback<MilepostShareJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        MainApiIO.getInstance().postRequest(HttpUrls.API_MILEPOST_SHARE_DATA, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("里程碑分享数据===onSuccess");

                MilepostShareJson mDataJson = JSON.parseObject(res, MilepostShareJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }
}
